<!DOCTYPE html>
<html lang="ch-zn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./node_modules/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="./node_modules/bootstrap-icons/font/bootstrap-icons.css">
    <title>借书页面</title>
    <script src="./node_modules/bootstrap/dist/js/bootstrap.bundle.js"></script>
    <style>
        * {
            user-select: none;
        }
    </style>
</head>

<body>
    <div class="container">
        <nav class="navbar navbar-expand-lg bg-body-tertiary">
            <div class="container-fluid">
                <a class="navbar-brand" href="./index.html">图书管理系统</a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">图书管理</a>
                            <ul class="dropdown-menu">
                                <li><a class="dropdown-item" href="./bookquery.html">图书查询</a></li>
                                <li><a class="dropdown-item" href="./bookchange.html">图书更改</a></li>
                            </ul>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">学生入口</a>
                            <ul class="dropdown-menu">
                                <li><a class="dropdown-item" href="./signup.html">新生注册</a></li>
                                <li>
                                    <hr class="dropdown-divider">
                                </li>
                                <li><a class="dropdown-item" href="./borrowbook.html">我要借书</a></li>
                                <li><a class="dropdown-item" href="./returnbook.html">我要还书</a></li>
                            </ul>
                        </li>
                    </ul>
                    <form class="d-flex" role="search">
                        <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
                        <button class="btn btn-outline-success" type="submit">Search</button>
                    </form>
                </div>

            </div>
        </nav>
        <br/>
        <div class="row">
            <div class="input-group mb-3">
                <span class="input-group-text">学生证号</span>
                <input type="text" class="form-control" placeholder="Id number" id="sid" aria-label="Id" aria-describedby="basic-addon1">
            </div>
        </div>
        <div class="row">
            <div class="input-group mb-3">
                <span class="input-group-text" id="basic-addon1">书籍编号</span>
                <input type="text" class="form-control" placeholder="Bookid" aria-label="Bookid" id="bookid" aria-describedby="basic-addon1">
                <span class="input-group-text text-bg-secondary">OR</span>
                <span class="input-group-text" id="basic-addon2">书籍名称</span>
                <input type="text" class="form-control" placeholder="Bookname" aria-label="Bookname" id="bookname" aria-describedby="basic-addon1">
            </div>
        </div>
        <div class="row">
            <div class="col"></div>
            <button type="button" class="btn btn-primary col-4" onclick="borrow()">借书</button>
            <div class="col"></div>
        </div>
    </div>
</body>
<script type="text/javascript" src="./node_modules/jquery/dist/jquery.js" charset="UTF-8"></script>
<script>
    var url0 = 'http://localhost:80/index.php/Mysql/'

    function borrow() {
        var input_stuid = document.getElementById("sid").value;
        var input_bookid = document.getElementById("bookid").value;
        var input_bookname = document.getElementById("bookname").value;
        if (input_bookid) {
            inputdata = {
                "input_stuid": input_stuid,
                "input_bookid": input_bookid
            }
            $.ajax({
                methods: "GET",
                url: url0 + "mysql_borrowbook_bybookid/",
                async: pageYOffset,
                data: inputdata,
                dataType: "json",
                contentType: "application/json;charset=UTF-8",
                success: function(data) {
                    if (data == "success") {
                        alert("借书成功！")
                    }
                    if (data.length > 12 && data.slice(0, 10) == 'date error') {
                        alert("您有书借阅时长超过" + data.slice(11) + "天,前去缴费！");
                    }
                    if (data == 'error2') {
                        alert("书籍已经接完了！");
                    }
                    if (data == 'error3') {
                        alert("学生不存在！");
                    }
                    if (data == 'error1') {
                        alert("查无此书！")
                    }
                },
                error: function(err) {
                    // 请求失败， err是失败的内容
                    print("请求失败");
                },
                complete: function(e) {
                    // 不管成功还是失败都会调用这个函数
                }
            })
        } else if (input_bookname) {
            inputdata = {
                "input_stuid": input_stuid,
                "input_bookname": input_bookname
            }
            $.ajax({
                methods: "GET",
                url: url0 + "mysql_borrowbook_bybookname/",
                async: pageYOffset,
                data: inputdata,
                dataType: "json",
                contentType: "application/json;charset=UTF-8",
                success: function(data) {
                    if (data == "success") {
                        alert("借书成功！")
                    }
                    if (data.length > 12 && data.slice(0, 10) == 'date error') {
                        alert("您有书借阅时长超过" + data.slice(11) + "天,前去缴费！");
                    }
                    if (data == 'error2') {
                        alert("书籍已经接完了！");
                    }
                    if (data == 'error3') {
                        alert("学生不存在！");
                    }
                    if (data == 'error1') {
                        alert("查无此书！")
                    }
                },
                error: function(err) {
                    // 请求失败， err是失败的内容
                    print("请求失败");
                },
                complete: function(e) {
                    // 不管成功还是失败都会调用这个函数
                }
            })
        } else {
            alert("书籍id和名称不得全为空，且学生id为必填项！");
            return;
        }
    }
</script>

</html>